/*
 * Copyright (c) 2020 hebeiyiliuba.com
 * All rights reserved.
 *
 */
package org.jeecg.modules.third.aipocr;

import com.baidu.aip.face.AipFace;
import com.baidu.aip.face.MatchRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.constant.enums.FaceImageTypeEnum;
import org.jeecg.common.constant.enums.FaceLivenessControlEnum;
import org.jeecg.common.constant.enums.FaceQualityControlEnum;
import org.jeecg.common.constant.enums.FaceTypeEnum;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * AipFace百度人脸识别
 * https://ai.baidu.com/ai-doc/FACE/8k37c1rqz#%E4%BA%BA%E8%84%B8%E5%AF%B9%E6%AF%94
 * @author duanlikao
 * @created 2020/12/30
 */
@Slf4j
@Service
public class AipFaceService {
    private static AipFace aipFaces ;


    private static final Logger LOGGER = LoggerFactory.getLogger(AipFaceService.class);

    private AipFaceService(){
        AipFactory aipFactory  =  new AipFactory();
        aipFaces = aipFactory.getAipFace();
    }

    /**
     * 人脸对比接口
     * @param imageBase64No1
     * @param imageBase64No2
     * @return
     */
   public String matchFace (String imageBase64No1 ,String imageBase64No2){

       if(StringUtils.isEmpty(imageBase64No1) || StringUtils.isEmpty(imageBase64No2) ){
           LOGGER.info("AipFaceService matchFace imageBase64No1: {} imageBase64No2: {}",
                   imageBase64No1,imageBase64No2);
           return null;
       }

       MatchRequest req1 = new MatchRequest(imageBase64No1,
               FaceImageTypeEnum.FACE_IMAGE_TYPE_BASE64.getMsg(),
               FaceTypeEnum.FACE_TYPE_IDCARD.getMsg(),
               FaceQualityControlEnum.FACE_QUALITY_CONTROL_TYPE_NONE.getMsg(),
               FaceLivenessControlEnum.FACE_LIVENESS_CONTROL_TYPE_NONE.getMsg()
               );

       MatchRequest req2 = new MatchRequest(imageBase64No2,
               FaceImageTypeEnum.FACE_IMAGE_TYPE_BASE64.getMsg(),
               FaceTypeEnum.FACE_TYPE_LIVE.getMsg(),
               FaceQualityControlEnum.FACE_QUALITY_CONTROL_TYPE_NONE.getMsg(),
               FaceLivenessControlEnum.FACE_LIVENESS_CONTROL_TYPE_NONE.getMsg()
               );
       ArrayList <MatchRequest> requests = new ArrayList<>();
       requests.add(req1);
       requests.add(req2);
       JSONObject resourcesJSONObject = aipFaces.match(requests);
       LOGGER.debug("AipFaceService matchFace resourcesJSONObject: {}",resourcesJSONObject.toString());
        return resourcesJSONObject.toString();

   }

//    public static void main(String[] args) {
//        AipFaceService aipFaceService= new AipFaceService();
//        String image1 ="/Users/duanlikao/Downloads/1侯相科/微信图片_20201027103911.jpg";
//        String image2 ="/Users/duanlikao/Downloads/11111.jpeg";
//
//        byte[] bytes = new byte[0];
//        try {
//            bytes = Util.readFileByBytes(image1);
//        } catch (IOException e) {
//            LOGGER.info("AipOcrService | idcard  file: {} | IOException:{} ",image1 , e.getMessage());
//            e.printStackTrace();
//        }
//
//        byte[] bytes1 = new byte[0];
//        try {
//            bytes1 = Util.readFileByBytes(image2);
//        } catch (IOException e) {
//            LOGGER.info("AipOcrService | idcard  file: {} | IOException:{} ",image2 , e.getMessage());
//            e.printStackTrace();
//        }
//
//        String imageBase64No1 = Base64Util.encode(bytes);
//        String imageBase64No2 = Base64Util.encode(bytes1);
//       String re =  aipFaceService.matchFace ( imageBase64No1 , imageBase64No2);
//       System.out.println(re);
//    }
}